package com.atguigu.dao.impl;

import com.atguigu.Bean.C_myOrder;
import com.atguigu.Bean.C_order;
import com.atguigu.Bean.C_user;
import com.atguigu.dao.OrderDao;

import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    /**
     * 交易后保存订单
     * @param c_order
     * @return
     */
    @Override
    public int saveOrder(C_order c_order) {
        String sql="insert into c_order values(?,?,?,?,?)";
        return updatedate(sql,c_order.getOrderId(),c_order.getCreateTime(),c_order.getPrice(),c_order.getStatus(),c_order.getUserId());
    }

    /**
     * 交易后用户查看【我的订单】
     * @param user_id
     * @return
     */
    @Override
    public List<C_myOrder> queryOrdersByUserId(Integer user_id,int begin,int pageSize) {
        String sql="SELECT a1.`name`,a1.`order_id`,a1.`price`,a1.`count`,a1.`total_price`,a.`create_time`,a.`status`" +
                " FROM c_order AS a " +
                " RIGHT JOIN c_order_item AS a1" +
                " ON a.`order_id`=a1.`order_id`" +
                " WHERE  a.`user_id`=?" +
                " ORDER BY a.`create_time` DESC"+
                " LIMIT ?,?;";
        return querydates(C_myOrder.class,sql,user_id,begin,pageSize);
    }
    /*
     *查询本用户所有的订单数
     */
    @Override
    public int showMyOrdersCount(Integer user_id) {
        String sql="SELECT COUNT(*)" +
                " FROM c_order AS a" +
                " RIGHT JOIN c_order_item AS a1" +
                " ON a.`order_id`=a1.`order_id`" +
                " WHERE a.`user_id`=?" +
                " ORDER BY a.`create_time` DESC";
        Object queryvalue = queryvalue(sql, user_id);
        return Integer.parseInt(String.valueOf(queryvalue));
    }

    /**
     *查询指定数量的，购买过商品的，用户们的信息
     */
    @Override
    public List<C_user> queryOrderUsers(int begin,int pageSize) {
        String sql="SELECT DISTINCT cu.*" +
                " FROM c_order AS co" +
                " INNER JOIN c_user AS cu" +
                " ON co.`user_id`=cu.`id`" +
                " LIMIT ?,?;";

        return querydates(C_user.class,sql,begin,pageSize);
    }
    /**
     *查询指定数量的，购买过商品的，用户们的总数量
     */
    @Override
    public int queryOrderUsersCount() {
        String sql="SELECT COUNT(DISTINCT cu.`id`)" +
                " FROM c_order AS co" +
                " INNER JOIN c_user AS cu" +
                " ON co.`user_id`=cu.`id`;";
        Object queryvalue = queryvalue(sql);
        return Integer.parseInt(String.valueOf(queryvalue));
    }
    /*
    修改订单状态（管理员:发货；用户:签收）
     */
    @Override
    public int changeOrderStatus(String order_id,int status) {
        String sql="UPDATE c_order SET `status`=? WHERE order_id=?";
        return updatedate(sql,status,order_id);
    }
}
